{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Moves to Convert String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minimumMoves"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #转换字符串的最少操作次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code> ，由 <code>n</code> 个字符组成，每个字符不是 <code>'X'</code> 就是 <code>'O'</code> 。</p>\n",
    "\n",
    "<p>一次<strong> 操作</strong> 定义为从 <code>s</code> 中选出 <strong>三个连续字符 </strong>并将选中的每个字符都转换为 <code>'O'</code> 。注意，如果字符已经是 <code>'O'</code> ，只需要保持 <strong>不变</strong> 。</p>\n",
    "\n",
    "<p>返回将 <code>s</code> 中所有字符均转换为 <code>'O'</code> 需要执行的&nbsp;<strong>最少</strong>&nbsp;操作次数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"XXX\"\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：<em>XXX</em></strong> -&gt; OOO\n",
    "一次操作，选中全部 3 个字符，并将它们转换为 <code>'O' 。</code>\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"XXOX\"\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：<em>XXO</em></strong>X -&gt; O<em><strong>OOX</strong></em> -&gt; OOOO\n",
    "第一次操作，选择前 3 个字符，并将这些字符转换为 <code>'O'</code> 。\n",
    "然后，选中后 3 个字符，并执行转换。最终得到的字符串全由字符 <code>'O'</code> 组成。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"OOOO\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>s 中不存在需要转换的 <code>'X' 。</code>\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>3 &lt;= s.length &lt;= 1000</code></li>\n",
    "\t<li><code>s[i]</code> 为 <code>'X'</code> 或 <code>'O'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-moves-to-convert-string](https://leetcode.cn/problems/minimum-moves-to-convert-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-moves-to-convert-string](https://leetcode.cn/problems/minimum-moves-to-convert-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"XXX\"', '\"XXOX\"', '\"OOOO\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        s = list(s)\n",
    "        ans = 0\n",
    "        for i, ch in enumerate(s):\n",
    "            if ch == 'X':\n",
    "                ans += 1\n",
    "                idx = i\n",
    "                while idx < n and idx - i < 3:\n",
    "                    s[idx] = '0'\n",
    "                    idx += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        c = -1\n",
    "        for i, v in enumerate(s):\n",
    "            if v == \"X\" and i > c:\n",
    "                ans += 1\n",
    "                c = i + 2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        times = 0\n",
    "        remain = 0\n",
    "        for char in s:\n",
    "            if char == 'X' and remain <= 0:\n",
    "                times += 1\n",
    "                remain = 2\n",
    "            else:\n",
    "                remain -= 1\n",
    "        return times\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        covered = -1\n",
    "        res=0\n",
    "        for i,c in enumerate(s):\n",
    "            if c=='X' and i>covered:\n",
    "                res +=1\n",
    "                covered = i+2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        covered = -1\n",
    "        res = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if c == 'X' and i > covered:\n",
    "                res += 1\n",
    "                covered = i + 2\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        # count = 0\n",
    "        # for i in range(0,len(s)-1,1):\n",
    "        #     if \"X\" in s[i:i+3]:\n",
    "        #         s = s[i:i+3].replace(\"X\",\"0\")\n",
    "        #         count += 1\n",
    "        # return count\n",
    "        i = 0\n",
    "        count = 0\n",
    "        while(i<len(s)):\n",
    "            if s[i] == 'X':\n",
    "                i += 3\n",
    "                count += 1\n",
    "            else:\n",
    "                i += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        _result = 0\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                _result += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return _result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        cnt = 0\n",
    "        s = list(s)\n",
    "        n = len(s)\n",
    "        for i in range(n-2):\n",
    "            if  s[i] == 'X':\n",
    "                cnt += 1\n",
    "                s[i] = s[i+1] = s[i+2] = 'O'\n",
    "        return cnt + int(s[-1] == 'X' or s[-2] == 'X')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans,l,idx = 0,len(s),s.find('X')\n",
    "        while idx < l:\n",
    "            if s[idx] == 'X':\n",
    "                idx += 3\n",
    "                ans += 1\n",
    "            else:\n",
    "                idx += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        i=0\n",
    "        num=0\n",
    "        while i<len(s):\n",
    "            if s[i]=='X':\n",
    "                num+=1\n",
    "                i+=3\n",
    "            else:\n",
    "                i+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        index = 0\n",
    "        ans = 0\n",
    "        while index < len(s):\n",
    "            if s[index] == \"X\":\n",
    "                ans += 1\n",
    "                index += 3\n",
    "            else:\n",
    "                index += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        tmp = list(s)\n",
    "        if set(tmp) == {'O'}:\n",
    "            return 0 \n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        res = 0\n",
    "        i = 0\n",
    "        while i<len(s):\n",
    "            if s[i] == \"X\":\n",
    "                res += 1\n",
    "                i += 2\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        count=0\n",
    "        if len(s)<4:\n",
    "            return 1\n",
    "        i=0\n",
    "        while i<len(s):\n",
    "            if s[i]==\"X\":\n",
    "                i+=3\n",
    "                count+=1\n",
    "            else:\n",
    "                i+=1\n",
    "        return count\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        i=0\n",
    "        ans=0\n",
    "        while i<n:\n",
    "            if s[i]=='X':\n",
    "                ans+=1\n",
    "                i+=3\n",
    "            else:\n",
    "                i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        r = 0\n",
    "        d = -1\n",
    "        for i, c in enumerate(s):\n",
    "            if c == 'X' and i > d:\n",
    "                r += 1\n",
    "                d = i + 2\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        c = 0\n",
    "        while True:\n",
    "            s = s.strip('O')\n",
    "            m = re.search(r'(XXX|XOX|XXO|XOO|XX|X)', s)\n",
    "            if not m:\n",
    "                break\n",
    "            a, b = m.span()\n",
    "            s = s[:a] + 'OOO' + s[b:]\n",
    "            c += 1\n",
    "        return c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        count = 0\n",
    "        i = 0\n",
    "        while i < (len(s)):\n",
    "            if s[i] == 'X':\n",
    "                count += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        t = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == 'X':\n",
    "                t+=1\n",
    "                i+=3\n",
    "        return t'''\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        i = 0\n",
    "        cnt = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                cnt += 1\n",
    "                i = i + 3\n",
    "            else: i += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 2\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        a = len(s)\n",
    "        ans = num = 0\n",
    "        while num < a:\n",
    "            if s[num] == 'X':\n",
    "                num += 3\n",
    "                ans += 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        t = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == 'X':\n",
    "                t+=1\n",
    "                i+=3\n",
    "        return t'''\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        cur = 0\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        while cur < n:\n",
    "            if s[cur] == 'X':\n",
    "                ans += 1\n",
    "                cur += 2\n",
    "            cur += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        c = 0\n",
    "        while True:\n",
    "            s = s.strip('O')\n",
    "            m = re.search(r'(XXX|XOX|XXO|OOX|OXO|XOO|OXX|XX|X)', s)\n",
    "            if not m:\n",
    "                break\n",
    "            a, b = m.span()\n",
    "            s = s[:a] + 'OOO' + s[b:]\n",
    "            c += 1\n",
    "        return c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        i=0\n",
    "        num=0\n",
    "        while i<len(s):\n",
    "            if s[i]=='X':\n",
    "                num+=1\n",
    "                i+=3\n",
    "            else:\n",
    "                i+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans, j = 0, 0\n",
    "        while j < len(s):\n",
    "            if s[j] == \"X\":\n",
    "                ans += 1\n",
    "                j += 3\n",
    "            else:\n",
    "                j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        count=0\n",
    "        while s.find('X')>-1:\n",
    "            count+=1\n",
    "            if s.find('X')+3>=len(s):\n",
    "                return count\n",
    "            else:\n",
    "                s=s[s.find('X')+3:]\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i<len(s):\n",
    "            if s[i]=='X':\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                i += 2\n",
    "                ans += 1\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        res, i = 0, 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                res += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans, j = 0, 0\n",
    "        while j < len(s):\n",
    "            if s[j] == \"X\":\n",
    "                ans += 1\n",
    "                j += 2\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        i=0\n",
    "        ans=0\n",
    "        while i<len(s):\n",
    "            if s[i]=='X':\n",
    "                i+=3\n",
    "                ans+=1\n",
    "            else:\n",
    "                i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        i=0\n",
    "        ans=0\n",
    "        while i<len(s):\n",
    "            if s[i]==\"X\":\n",
    "                ans+=1\n",
    "                i+=2\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        count=0\n",
    "        i=0\n",
    "        while i<len(s):\n",
    "            if s[i]=='X':\n",
    "                count+=1\n",
    "                i+=3\n",
    "                continue\n",
    "            i+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        count=0\n",
    "        i=0\n",
    "        while i < len(s):            \n",
    "            if s[i]=='X':\n",
    "                count+=1\n",
    "                i+=3\n",
    "            else:               \n",
    "                i+=1            \n",
    "        return count\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ret,pre = 0,-10\n",
    "        for i,c in enumerate(s):\n",
    "            if c=='X' and pre+2<i:\n",
    "                pre,ret = i,ret+1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        lenth=len(s)\n",
    "        count=0\n",
    "        i=0\n",
    "        while i<lenth:\n",
    "            index=s[i::].find('X')\n",
    "            if index==-1:\n",
    "                break\n",
    "            count+=1\n",
    "            i=i+3+index\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        covered=-1\n",
    "        res=0\n",
    "        for i,c in enumerate(s):\n",
    "            if c=='X' and i>covered:\n",
    "                res+=1\n",
    "                covered=i+2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        count = 0\n",
    "        ans = 0\n",
    "        while count < len(s):\n",
    "            if \"X\" == s[count]:\n",
    "                count += 3\n",
    "                ans += 1\n",
    "            else:\n",
    "                count += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        最少操作次数 = 0\n",
    "        x = 0\n",
    "        while x < n:\n",
    "            if s[x] == \"X\":\n",
    "                x += 3\n",
    "                最少操作次数 += 1\n",
    "            else:\n",
    "                x += 1 \n",
    "        return 最少操作次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        if len(s)<3:return 0\n",
    "        covered = -1\n",
    "        res = 0\n",
    "        for i,c in enumerate(s):\n",
    "            if c=='X' and i>covered:\n",
    "                res += 1\n",
    "                covered = i+2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        res=0\n",
    "        i=0\n",
    "        while i<n:\n",
    "            if s[i]==\"X\":\n",
    "                res+=1\n",
    "                i+=3\n",
    "            else:\n",
    "                i+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        s = list(s)\n",
    "        n = len(s)\n",
    "        res = 0\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if s[i] == 'X':\n",
    "                res += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "      count = 0\n",
    "      left = 0\n",
    "      right = left + 2\n",
    "      _len = len(s)\n",
    "\n",
    "      while right < _len:\n",
    "        _s = s[left:right+1]\n",
    "        print(_s)\n",
    "\n",
    "        if s[left] == 'X':\n",
    "          count += 1\n",
    "          left = right + 1\n",
    "          right = left + 2\n",
    "          continue\n",
    "        \n",
    "        left += 1\n",
    "        right += 1\n",
    "\n",
    "      \n",
    "      _s = s[left:]\n",
    "      print(f'last {_s}')\n",
    "\n",
    "      if _s is not None and 'X' in _s:\n",
    "        count += 1\n",
    "\n",
    "      return count\n",
    "      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        s_list = list(s)\n",
    "        n = len(s_list)\n",
    "        count = 0\n",
    "        # print(s_list)\n",
    "        if (n-3) >= 1:\n",
    "            for i in range(n-2):\n",
    "                if s_list[i] == \"X\":\n",
    "                    count += 1\n",
    "                    s_list[i], s_list[i+1], s_list[i+2] = \"O\", \"O\", \"O\"\n",
    "        else:\n",
    "            if 'X' in s:\n",
    "                return 1\n",
    "            else:\n",
    "                return 0 \n",
    "        \n",
    "        if \"X\" in s_list[-2:]:\n",
    "            count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        transform = 0\n",
    "        while 1:\n",
    "            while 1:\n",
    "                if s == None or len(s) == 1:\n",
    "                    break\n",
    "                if s[0] == \"O\":\n",
    "                    s = s[1:]\n",
    "                else:\n",
    "                    break\n",
    "            \n",
    "            if len(s) % 3 == 0:\n",
    "                s = s\n",
    "            elif len(s) % 3 == 1:\n",
    "                s = s + \"OO\"\n",
    "            elif len(s) % 3 == 2:\n",
    "                s = s + \"O\"\n",
    "\n",
    "            if s[0:3] != \"OOO\" and len(s) != 3:\n",
    "                transform = transform + 1\n",
    "                s=s[3:]\n",
    "            elif s[0:3] != \"OOO\" and len(s) == 3:\n",
    "                transform = transform + 1\n",
    "                return transform\n",
    "            elif len(s) == 3:\n",
    "                return transform\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        s = list(s)\n",
    "        ret = 0\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            if s[i] == 'X':\n",
    "                ret += 1\n",
    "                if i + 1 < n:\n",
    "                    s[i + 1] = 'O'\n",
    "                if i + 2 < n:\n",
    "                    s[i + 2] = 'O'\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n",
    "\n",
    "        # def zhuan(str):\n",
    "        #     for i in range(3):\n",
    "        #         str[i]='O'\n",
    "        #      return str\n",
    "        # str=list(input())\n",
    "        # len=len(str)\n",
    "        # count=0\n",
    "        # for i in range(len):\n",
    "        #     if str[i]=='X':\n",
    "        #         str=zhuan(str)\n",
    "        #         i=i+3\n",
    "        #         count+=1\n",
    "        # print(count)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        covered = -1\n",
    "        ans = 0\n",
    "        for i,c in enumerate(s):\n",
    "            if c == \"X\" and i > covered:\n",
    "                ans += 1\n",
    "                covered = i + 2\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        ans = i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"X\":\n",
    "                ans += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        covered = -1\n",
    "        res = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if c == 'X' and i > covered:\n",
    "                res += 1\n",
    "                covered = i + 2\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        pointer = 0\n",
    "        answer = 0\n",
    "        while pointer < n:\n",
    "            if s[pointer] == 'X':\n",
    "                pointer += 3\n",
    "                answer += 1\n",
    "            else:\n",
    "                pointer += 1\n",
    "        return answer    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        cnt = 0\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == 'X':\n",
    "                cnt += 1\n",
    "                i += 3\n",
    "            else:\n",
    "                i += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        i=count=0\n",
    "        n = len(s)\n",
    "        while i<n:\n",
    "            if s[i]==\"X\":\n",
    "                count+=1\n",
    "                i+=2\n",
    "            i+=1\n",
    "        return count\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        index = 0\n",
    "        while index < n:\n",
    "            if s[index] == 'X':\n",
    "                ans += 1\n",
    "                index += 3\n",
    "            else:\n",
    "                index += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumMoves(self, s: str) -> int:\n",
    "        a=i=0\n",
    "        while i <len(s):\n",
    "            if s[i] == \"X\":\n",
    "                a+=1\n",
    "                i+=3\n",
    "            else:\n",
    "                i+=1\n",
    "        return a\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
